<?php

function imageoutput($filepath, $ext, $title, $text) {

	if (empty($title)) { $title = '123456789+123456789+123456789+12'; }
	if (empty($text)) { $text = '123456789+123456789+123456789+123455679+'; }
	$result = true;
	$errorText = '';
	$imageWidth = 490;
	$imageHeight = 256;
	$imageTopBoundery = 63;
	$imageRepeatHeight = 12;
	$imageRepeat = 4;
	$imageBottomHeight = 42;
	

	$textSpaceHeight = 70;
	$titleFontSize = 20.0;
	$textFontSize = 15.0;
	$fontFile = 'arial.ttf';

	$fontProfile = 5;

	$bcenterImagePath = $filepath;
	if (!file_exists($filepath)) {
		$bcenterImagePath = 'filenotfound.jpg'; 
		$result = false;
		$errorText = 'File not found.';
	}

	$dynWidth = 446;
	$dynTopBeiselHeight = 39;
	$dynBottomBeiselHeight = 28;
	$dynX = 22;
	$dynY = 24;

	putenv('GDFONTPATH=' . realpath('.'));

	$imageRepeatCursor = 0;
	$bcenter = null;
	switch ($ext) {
	case 'gif':
		$bcenter = imagecreatefromgif($bcenterImagePath);
		break;
	case 'bmp':
		$bcenter = imagecreatefromwbmp($bcenterImagePath);
		break;
	case 'png':
		$bcenter = imagecreatefrompng($bcenterImagePath);
		break;
	case 'jpg':
	case 'jpeg':
	default:
		$bcenter = imagecreatefromjpeg($bcenterImagePath);
		break;
	}

	if (!$bcenter) {
		$bcenter = imagecreatefromjpeg('unabletoload.jpg');
		$bcenterImagePath = 'unabletoload.jpg';
		$errorText = 'Unable to load .'.$ext;
		$result = false;
	}

	$bcenterSize = getimagesize($bcenterImagePath);
	$bcenterW = $bcenterSize[0];
	$bcenterH = $bcenterSize[1];

	$bcenterRw = $dynWidth;
	$bcenterRh = ($bcenterH * $dynWidth)/$bcenterW;

	$bcenterDest = imagecreatetruecolor($bcenterRw, $bcenterRh);
	imagecopyresampled($bcenterDest, $bcenter, 0, 0, 0, 0, $bcenterRw, $bcenterRh, $bcenterW, $bcenterH);

	$imageRepeatReal = ($bcenterRh-$dynTopBeiselHeight-$dynBottomBeiselHeight)/$imageRepeatHeight;
	$imageRepeat = floor($imageRepeatReal);
	$imageRepeatLastHeight = ($imageRepeatReal - floor($imageRepeatReal)) * $imageRepeatHeight; 
	$imageHeight = $imageTopBoundery + $imageBottomHeight + ($imageRepeatReal*$imageRepeatHeight) + $textSpaceHeight;



	$image = imagecreatetruecolor($imageWidth, $imageHeight);
	imagealphablending($image, false);

	$backgroundColor = imagecolorallocatealpha($image, 0, 0, 0, 0);
	imagefilledrectangle($image, 0, 0, $imageWidth, $imageHeight, $backgroundColor);

	imagealphablending($image, true);
	imagesavealpha($image, true);

	$btop = imagecreatefrompng("top.png");
	$brepeat = imagecreatefrompng("der-izq.png");
	$bbottom = imagecreatefrompng("bottom.png");

	imagecopy($image, $bcenterDest, $dynX, $dynY, 0, 0, $bcenterRw, $bcenterRh);

	imagecopy($image, $btop, 0, 0, 0, 0, $imageWidth, $imageTopBoundery);

	for ($i=0;$i<$imageRepeat; $i++) {
		$imageRepeatCursor = $imageTopBoundery + $i * $imageRepeatHeight;
		imagecopy($image, $brepeat, 0, $imageRepeatCursor, 0, 0, $imageWidth, $imageRepeatHeight);
	}
	$imageRepeatCursor = $imageTopBoundery + $imageRepeat * $imageRepeatHeight;
	imagecopy($image, $brepeat, 0, $imageRepeatCursor, 0, 0, $imageWidth, $imageRepeatLastHeight);
	$imageRepeatCursor = $imageTopBoundery + $imageRepeatReal * $imageRepeatHeight;

	imagecopy($image, $bbottom, 0, $imageRepeatCursor, 0, 0, $imageWidth, $imageBottomHeight);

	if (function_exists('imagettfbbox')) {

		$resTitleBbox = imagettfbbox($titleFontSize, 0, $fontFile, $title);
		$titleX = $resTitleBbox[0] + $imageWidth/2 - $resTitleBbox[4]/2;
		$titleY = $imageRepeatCursor + $dynBottomBeiselHeight + $titleFontSize + 21;
		imagettftext($image, $titleFontSize, 0, $titleX, $titleY, imagecolorallocatealpha($image, 212, 250, 7, 0), $fontFile, $title);

		$resTextBbox = imagettfbbox($textFontSize, 0, $fontFile, $text);
		$textX = $resTextBbox[0] + $imageWidth/2 - $resTextBbox[4]/2;
		$textY = $titleY + $textFontSize + 10;
		imagettftext($image, $textFontSize, 0, $textX, $textY, imagecolorallocatealpha($image, 255, 7, 210, 0), $fontFile, $text);
	
	} else {
		$titleWidth = imagefontwidth($fontProfile)*strlen($title);
		$titleHeight = imagefontheight($fontProfile);
		$titleImage = imagecreatetruecolor($titleWidth, $titleHeight);
		imagealphablending($titleImage, false);
		imagefilledrectangle($titleImage, 0, 0, $titleWidth, $titleHeight, $backgroundColor);
		imagealphablending($titleImage, true);
		imagesavealpha($titleImage, true);
		$titleScale = 1.7;
	
		$titleDX = (($imageWidth - $titleWidth*$titleScale)/2);
		$titleDY = $imageRepeatCursor + 80;
		
		imagestring($titleImage, $fontProfile, 0, 0, $title, imagecolorallocatealpha($image, 212, 250, 7, 0));
		//imagecopyresampled($bcenterDest, $bcenter, 0, 0, 0, 0, $bcenterRw, $bcenterRh, $bcenterW, $bcenterH);
		imagecopyresampled($image, $titleImage, $titleDX, $titleDY, 0, 0, $titleWidth*$titleScale, $titleHeight*$titleScale, $titleWidth, $titleHeight);

		$textWidth = imagefontwidth($fontProfile)*strlen($text);
		$textDX = ($imageWidth - $textWidth)/2;
		$textDY = $imageRepeatCursor + 40;
		imagestring($image, $fontProfile, $textDX, $textDY, $text, imagecolorallocatealpha($image, 255, 7, 210, 0));
	}

	if (!empty($errorText)) { 
		imagestring($image, 2, 41, 41, $errorText, imagecolorallocatealpha($image, 255, 255, 255, 0));
		imagestring($image, 2, 40, 40, $errorText, imagecolorallocatealpha($image, 0, 0, 0, 0)); 
	}

	//echo "Here";
	//return;

	header('Content-Type: image/png');

	imagepng($image);

	imagedestroy($image);
	imagedestroy($btop);
	imagedestroy($brepeat);
	imagedestroy($bbottom);
	imagedestroy($bcenter);
	imagedestroy($bcenterDest);

	//if ($result) unlink($filepath);
	return $result;
}
?>
